package tu.appengine.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.Path;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Path("student")
public class StudentDAO {
	private static StudentDAO instance;
	private final static String STUDENT_ENTITY_TYPE = "Student";
	private final static String GRADE_ENTITY_TYPE = "Grade";
	private final static String ATTENDANCE_ENTITY_TYPE = "Attendance";
	
	private StudentDAO() {
	}
	
	public static StudentDAO getInstance() {
		if(instance == null) {
			instance = new StudentDAO();
		}
		return instance;
	}
	
	public Student retrieveStudent(String studentId, String personalId) {		
//		BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
//		String pdf = blobstoreService.createUploadUrl("/upload");
//		System.out.println(pdf);
		
		DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		String keyName = studentId + "-" + personalId;
		Key key = KeyFactory.createKey(STUDENT_ENTITY_TYPE, keyName);
		
		Student student = null;
		try {
			Entity en = datastoreService.get(key);
			student = new Student();
			
			student.setStudentId((String)en.getProperty("studentId"));
			student.setPersonalId((String)en.getProperty("personalId"));
			student.setFirstName((String)en.getProperty("firstName"));
			student.setLastName((String)en.getProperty("lastName"));
			student.setFaculty((String)en.getProperty("faculty"));
			student.setGroup((Long)en.getProperty("group"));
			student.setPotok((Long)en.getProperty("potok"));
			student.setSemester((Long)en.getProperty("semester"));
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
		}		
		
		return student;
	}
	
	public List<Grade> retrieveGrades(String studentId) {		
		DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		Key key = KeyFactory.createKey(GRADE_ENTITY_TYPE, studentId);
		
		List<Grade> grades = new ArrayList<Grade>();
		try {
			Entity en = datastoreService.get(key);
			Map<String, Object> properties = en.getProperties();
			Set<String> propNames = properties.keySet();
			for (String string : propNames) {
				Long value = (Long)en.getProperty(string);
				grades.add(new Grade(string, value));
			}
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
		}
		
		return grades;
	}
	
	public List<Attendance> retrieveAttendances(String studentId) {
		DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		Key key = KeyFactory.createKey(ATTENDANCE_ENTITY_TYPE, studentId);
		
		List<Attendance> attendances = new ArrayList<Attendance>();
		try {
			Entity en = datastoreService.get(key);
			Map<String, Object> properties = en.getProperties();
			Set<String> propNames = properties.keySet();
			for (String string : propNames) {
				List<Long> values = (List<Long>)en.getProperty(string);
				attendances.add(new Attendance(string, values));
			}
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
		}
		
		return attendances;
	}
	
	public void insertTestStudent(String studentId, String personalId) {
		insertRandomStudent(studentId, personalId);
		insertRandomAttendance(studentId);
		insertRandomGrades(studentId);
	}
	
	private void insertRandomStudent(String studentId, String personalId) {
		DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		String keyName = studentId + "-" + personalId;
		Key key = KeyFactory.createKey(STUDENT_ENTITY_TYPE, keyName);
		Entity student = new Entity(key);
		
		student.setProperty("studentId", studentId);
		student.setProperty("personalId", personalId);
		student.setProperty("firstName", "First");
		student.setProperty("lastName", "Last");
		student.setProperty("faculty", "FKSU");
		student.setProperty("group", 55L);
		student.setProperty("potok", 10L);
		student.setProperty("semester", 8L);
		
		datastoreService.put(student);
	}
	
	private void insertRandomGrades(String studentId) {
		DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		Key key = KeyFactory.createKey(GRADE_ENTITY_TYPE, studentId);
		
		Entity entity = new Entity(key);
		entity.setProperty("Math1", 6l);
		entity.setProperty("Math2", 5l);
		entity.setProperty("Math3", 4l);
		entity.setProperty("Math4", 3l);
		entity.setProperty("Math5", 2l);
		
		datastoreService.put(entity);
	}
	
	private void insertRandomAttendance(String studentId) {
		DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		Key key = KeyFactory.createKey(ATTENDANCE_ENTITY_TYPE, studentId);
		Entity entity = new Entity(key);
		
		List<Long> attendance = new ArrayList<Long>();
		attendance.add(1L);
		attendance.add(3L);
		attendance.add(4L);
		entity.setProperty("Math1", attendance);
		entity.setProperty("Math2", attendance);
		
		datastoreService.put(entity);
	}
}
